package netudp;
/**
 * UDP传输层
 */

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class DatagramReceive {
	

	public static void main(String[] args) throws SocketException {
		try {
			DatagramSocket ds=new DatagramSocket(9988);//创建socket指定端口号
			byte[] buf=new byte[1024];//缓冲区
			DatagramPacket dp=new DatagramPacket(buf,1024);
			ds.receive(dp);//接收
			String s=new String(dp.getData(),0,dp.getLength());
			System.out.println("发送方说:"+s);
			//回发
			//dp.setData("我很好！".getBytes());
			DatagramPacket outp=new DatagramPacket(buf, 1024,dp.getAddress(),dp.getPort());
			outp.setData("我很好".getBytes());
			ds.send(outp);
			ds.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
